﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 护士工作站
{
  
    public partial class 出院 : Form
    {
        SqlConnection conn = new SqlConnection(DBHelper.connString);
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        public 出院()
        {
            InitializeComponent();
        }
        public void Fill()
        {
            string sql = "select InHospital.HosNo,Bed.BedNo,PatName,PatSex,PatType,HosSurgerydate,BedSubject,HosArea from InHospital,Bed,Patient where InHospital.HosNo=Patient.HosNo and InHospital.BedNo=Bed.BedNo";
            if (cb_sstj.Text== "床号")
                sql += " and Bed.BedNo like '%" + tb_sstj.Text.Trim() + "%'";
            else 
                  if (cb_sstj.Text == "住院号")
                      sql += " and InHospital.HosNo like '%" + tb_sstj.Text.Trim() + "%'";
           // sql += "order by InHospital.HosNo desc";
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                da = new SqlDataAdapter(sql,conn );
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
        }

        private void 出院_Load(object sender, EventArgs e)
        {
            Fill();
        }

        private void 取消_Click(object sender, EventArgs e)
        {
            this.Hide();
            菜单页面 f = new 菜单页面();
            f.Show();
        }

        private void bt_ss_Click(object sender, EventArgs e)
        {
            Fill();
        }

        private void 确定_Click(object sender, EventArgs e)
        {
            int i = 0;
            int j = 0;
            int z = 0;
            //try
          //  {
            //    i = (int)dataGridView1.CurrentRow.Cells[0].Value;

           // }
          //  catch (System.Exception ex)
          //  {
           //     MessageBox.Show("请选择有效数据行！");
           //     return;
           // }
            if (MessageBox.Show("确定要删除吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                return;
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string sql = string.Format("delete from InHospital where HosNo={0}", i);
                string str = string.Format("delete from Patient where HosNo={0}", j);
                string abc = string.Format("delete from Bed where BedNo={0}", z);
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlCommand com = new SqlCommand(str, conn);
                SqlCommand bed = new SqlCommand(abc, conn);
                conn.Open();
                int n = Convert.ToInt32(cmd.ExecuteNonQuery());
                int m = Convert.ToInt32(com.ExecuteNonQuery());
                int x = Convert.ToInt32(bed.ExecuteNonQuery());
                if (n == 1 && m == 1 && z == 1)
                {
                    MessageBox.Show("删除成功！");
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
            Fill();
        }
    }
}
